 package com.ssm.sample.controller.controllers;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ssm.sample.controller.base.BaseController;
import com.ssm.sample.facade.facades.Facades;
import com.ssm.sample.facade.facades.YXQFacades;
import com.ssm.sample.util.PageData;


/**
 * 
 * 
 * 44444
 * @author 卖糖果的坏小孩
 *
 */
@Controller
@RequestMapping("/YXQ")
public class YXQController extends BaseController
{//1
	@Autowired
  private YXQFacades yxqfacades;
	
	/**
	 * 查询所有成员的信息
	 * @return
	 */
	  @RequestMapping("/Test")
	  public ModelAndView Test()
	  {
	    ModelAndView mv = this.getModelAndView();
	    PageData pd = new PageData();
	    try
	    {
	      pd = this.getPageData();
	      List<PageData> list = yxqfacades.selectAll(pd);
	      mv.addObject("list",list);
	    }catch(Exception e)
	    {
	      logger.error(e);
	      e.printStackTrace();
	    }
	    mv.setViewName("/YXQ/Test");
	    return mv;
	  }
	  
	  /**
	   * Test是路径
	   * 查询一条成员信息
	   * @return
	   */
		@RequestMapping("/TestSelectOneByIdPage")
		public ModelAndView selectOneById() {
			ModelAndView mv = this.getModelAndView();
			PageData pd =null;
			try {
				pd = this.getPageData();
				boolean result=false;
				String selectID = (String) pd.get("selectID");			
				PageData empPD =yxqfacades.selectOneById(selectID);	//empPD是Map类型
				if(empPD!=null) {
					result=true;
				}
				mv.addObject("empPD", empPD);
				mv.addObject("selectID", selectID);
				mv.addObject("result",result);//用于界面间数据的传递
				//输出打印结果
//				 <c:forEach items="${empPD}" var="item" varStatus="status">
//					<h1>${(status.index+1)}--姓名：${item.name}</h1>
//				</c:forEach>
				
				System.out.println("empPD="+empPD.toString());	
				
				 //对empPD进行遍历，得到每个属性的具体信息
				 
				/**
				 *java8 Lambda
                 *java8提供了Lambda表达式支持，语法看起来更简洁，可以同时拿到key和value，不过，经测试，性能低于entrySet,所以更推荐用entrySet的方式
				 */
//				 empPD.forEach((key, value) -> {
//					 if(key.equals("name"))
//			            {System.out.println(key + ":" + value);
//			            String name=value.toString();
//			            mv.addObject("name", name);//将参数传递给测试界面
//			            }
//			        });
				//System.out.println("result="+result);
				//System.out.println("selectID="+selectID.toString());
		    }catch(Exception e)
		    {
		      logger.error(e);
		      e.printStackTrace();
		    }
			mv.addObject("pd", pd);
			//System.out.println("pd="+pd.toString()); 		
		    mv.setViewName("/YXQ/TestSelectOneByIdPage");
		    return mv;
		}	
		
		/**
		 * 删除一条成员信息
		 * @return
		 */
		@RequestMapping("/TestDeletePage") // ****************ProxyBrand
		public ModelAndView TestDeletePage() {
			ModelAndView mv = this.getModelAndView();
			PageData pd = null;
			try {
				pd = this.getPageData();
				String memberId = (String) pd.get("memberId");
				boolean result = yxqfacades.deleteMemberById(memberId);
				mv.addObject("result", result);
				mv.addObject("memberId", memberId);
			} catch (Exception e) {
				logger.error(e);
				e.printStackTrace();
			}
			mv.setViewName("/YXQ/TestDeletePage");
			return mv;
		}
		
		/**
		 * 添加一个成员信息
		 * @return
		 */
		@ResponseBody
		@RequestMapping(value = "/TestInsertOneByIdPage")
		public Object insertOneById() {
			Map<String, Object> result = new HashMap<String, Object>();
			PageData pd = new PageData();
			try {
				pd = this.getPageData();
				boolean b = this.yxqfacades.insertOneById(pd);
				if (b) {
					result.put("status", "OK");
				} else {
					result.put("status", "ERROR");
				}
				System.out.println("b="+b);
				System.out.println("pd="+pd.toString());
			} catch (Exception e) {
				logger.error(e);
				e.printStackTrace();
				result.put("status", "ERROR");
			}
			System.out.println("result="+result.toString());
			return result;
		}
		/**
		 * 更新一条成员信息
		 * @return
		 */
		@ResponseBody
		@RequestMapping(value = "/TestUpdateOneByIdPage")
		public Object updateOneById() {
			Map<String, Object> result = new HashMap<String, Object>();
			PageData pd = new PageData();
			try {
				pd = this.getPageData();
				boolean b = this.yxqfacades.updateOneById(pd);
				if (b) {
					result.put("status", "OK");
				} else {
					result.put("status", "ERROR");
				}
				System.out.println("b="+b);
				System.out.println("pd="+pd.toString());
			} catch (Exception e) {
				logger.error(e);
				e.printStackTrace();
				result.put("status", "ERROR");
			}
			System.out.println("result="+result.toString());
			return result;
		}
		
		/**
		 * 连接index_1
		 * @return
		 */
		@RequestMapping("/index_1")
		public ModelAndView index_1()
		  {
		    ModelAndView mv = this.getModelAndView();
		    PageData pd = new PageData();
		    mv.setViewName("/YXQ/index_1");
		    return mv;
		  }
		/**
		 * 查询所有成员的信息
		 * @return
		 */
		  @RequestMapping("/indexAll")
		  public ModelAndView indexAll()
		  {
		    ModelAndView mv = this.getModelAndView();
		    PageData pd = new PageData();
		    try
		    {
		      pd = this.getPageData();
		      List<PageData> list = yxqfacades.selectAll(pd);
		      mv.addObject("list",list);
		    }catch(Exception e)
		    {
		      logger.error(e);
		      e.printStackTrace();
		    }
		    mv.setViewName("/YXQ/indexAll");
		    return mv;
		  }
		/**
		 * 连接main
		 * @return
		 */
		@RequestMapping("/main")
		public ModelAndView main()
		  {
		    ModelAndView mv = this.getModelAndView();
		    PageData pd = new PageData();
		    mv.setViewName("/YXQ/main");
		    return mv;
		  }
		
		/**
		 * 连接ShiXian
		 * @return
		 */
//		@RequestMapping("/ShiXian")
//		public ModelAndView ShiXian()
//		  {
//		    ModelAndView mv = this.getModelAndView();
//		    PageData pd = new PageData();
//		    mv.setViewName("/YXQ/ShiXian");
//		    return mv;
//		  }
		
		/**
		 * 连接addshopping.jsp
		 * @return
		 */
		@RequestMapping("/addshopping")
		public ModelAndView addshopping()
		  {
		    ModelAndView mv = this.getModelAndView();
		    PageData pd = new PageData();
		    mv.setViewName("/YXQ/addshopping");
		    return mv;
		  }
		
		
		/**
		 * 连接login_failure.jsp
		 * @return
		 */
		@RequestMapping("/login_failure")
		public ModelAndView login_failure()
		  {
		    ModelAndView mv = this.getModelAndView();
		    PageData pd = new PageData();
		    mv.setViewName("/YXQ/login_failure");
		    return mv;
		  }
		
		  /**
		   * 用于登录界面的验证
		   * @return
		   */
			@RequestMapping("/ShiXian")
			public ModelAndView selectOneByName() {
				ModelAndView mv = this.getModelAndView();
				PageData pd =null;String selectName = "";
				boolean result=false;
				try {
					pd = this.getPageData();
					
					selectName = (String) pd.get("selectName");			
					PageData empPD =yxqfacades.selectOneByName(selectName);	//empPD是Map类型
					if(empPD!=null) {
						result=true;
					}
					mv.addObject("userName",selectName);
					mv.addObject("empPD", empPD);
					mv.addObject("selectName", selectName);
					mv.addObject("result",result);//用于界面间数据的传递				
					System.out.println("empPD="+empPD.toString());	
			    }catch(Exception e)
			    {
			      logger.error(e);
			      e.printStackTrace();
			    }
				mv.addObject("pd", pd);		
				if(result) {
					mv.setViewName("/YXQ/main");	
				}else {
					mv.setViewName("/YXQ/ShiXian");
				}
			    
			    return mv;
			}	
}